package com.webcash.CNBoard.Controller;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.webcash.CNBoard.Model.CalendarModel;
import com.webcash.CNBoard.Model.Login;
import com.webcash.CNBoard.Model.MyProject;
import com.webcash.CNBoard.Model.Project;
import com.webcash.CNBoard.Model.Work;
import com.webcash.CNBoard.Model.WorkTracer;
import com.webcash.CNBoard.Service.ProjectService;
import com.webcash.CNBoard.util.CalendarUtil;

@Controller
@RequestMapping("/project/gantt")
@SessionAttributes("login")
public class ProjectGanttChartController {
	@Autowired
	MyProject myProject;
	@Autowired
	ProjectService projectService;

	@RequestMapping
	public String action(@ModelAttribute Login login, Model model) {
		CalendarUtil calendarUtil = new CalendarUtil();
		CalendarModel cal = calendarUtil.calendar();
		Calendar todayCal = Calendar.getInstance();
		todayCal.set(cal.getCurrentYear(), cal.getCurrentMonth(), 1);
		cal.setCol(todayCal.get(todayCal.DAY_OF_WEEK) - 1);
		List<Work> workList = projectService
				.getWorkTracerListByProjectNo(myProject.getProjectNo());
		model.addAttribute("calendar", cal);
		model.addAttribute("workList", workList);
		System.out.println("aa" +workList);
		return "project/gantt";
	}

	@RequestMapping(value = "changeMonth")
	public String changeAction(@ModelAttribute Login login,
			@RequestParam int year, @RequestParam int month, Model model) {
		CalendarUtil calendarUtil = new CalendarUtil(year, month);
		CalendarModel cal = calendarUtil.calendar();
		Calendar todayCal = Calendar.getInstance();
		todayCal.set(cal.getCurrentYear(), cal.getCurrentMonth(), 1);
		cal.setCol(todayCal.get(todayCal.DAY_OF_WEEK) - 1);

		List<Work> workList = projectService
				.getWorkTracerListByProjectNo(myProject.getProjectNo());

		model.addAttribute("calendar", cal);
		model.addAttribute("workList", workList);
		return "project/gantt";
	}

	@RequestMapping(value = "year")
	public String yearAction(@ModelAttribute Login login,
			@RequestParam int year, @RequestParam int month, Model model) {
		CalendarUtil calendarUtil = new CalendarUtil(year, month);
		CalendarModel cal = calendarUtil.calendar();

		int[] lastDay = new int[12];

		for (int i = 0; i < lastDay.length; i++) {
			Calendar lastCalendar = Calendar.getInstance();

			lastCalendar.set(lastCalendar.get(Calendar.YEAR), i, 1);
			lastDay[i] = lastCalendar.getActualMaximum(lastCalendar.DATE);

		}

		List<Work> workList = projectService
				.getWorkTracerListByProjectNo(myProject.getProjectNo());

		for (int i = 0; i < workList.size(); i++) {
			Calendar calendar1 = Calendar.getInstance();
			calendar1.setTime(workList.get(i).getWorkStartTime());
			workList.get(i).setStartDate(calendar1.get(calendar1.DAY_OF_YEAR));
			Calendar calendar2 = Calendar.getInstance();
			calendar2.setTime(workList.get(i).getWorkDeadTime());
			workList.get(i).setDeadDate(calendar2.get(calendar1.DAY_OF_YEAR));
		}
		model.addAttribute("lastDay", lastDay);
		model.addAttribute("calendar", cal);
		model.addAttribute("workList", workList);

		return "project/ganttYear";
	}

	@RequestMapping(value = "changeYear")
	public String yearChangeAction(@ModelAttribute Login login,
			@RequestParam int year, Model model) {

		System.out.println(year);
		CalendarUtil calendarUtil = new CalendarUtil(year, 11);
		CalendarModel cal = calendarUtil.calendar();

		int[] lastDay = new int[12];

		for (int i = 0; i < lastDay.length; i++) {
			Calendar lastCalendar = Calendar.getInstance();

			lastCalendar.set(year, i, 1);
			lastDay[i] = lastCalendar.getActualMaximum(lastCalendar.DATE);

		}

		List<Work> workList = projectService
				.getWorkTracerListByProjectNo(myProject.getProjectNo());

		for (int i = 0; i < workList.size(); i++) {
			Calendar calendar1 = Calendar.getInstance();
			calendar1.setTime(workList.get(i).getWorkStartTime());
			workList.get(i).setStartDate(calendar1.get(calendar1.DAY_OF_YEAR));
			Calendar calendar2 = Calendar.getInstance();
			calendar2.setTime(workList.get(i).getWorkDeadTime());
			workList.get(i).setDeadDate(calendar2.get(calendar1.DAY_OF_YEAR));
		}

		model.addAttribute("calendar", cal);
		model.addAttribute("workList", workList);
		model.addAttribute("lastDay", lastDay);
		return "project/ganttYear";

	}
}
